import os

def gen(model):
    cur = os.getcwd()
    pjRoot = os.path.split(cur)[0]
    templatePath = os.path.join(pjRoot, 'application', 'templates', model)

    for html in allHtmls(templatePath):
        f = open(os.path.join(templatePath, html), 'r+')
        content = f.readlines()
        f.seek(0)
        if f.read(1) != '{':
            f.seek(0)
            f.write(headerContent())
            for c in content:
                f.write(c)
            f.write(footerContent())
            f.close()
    
    
def headerContent():
    return '{% extends "../base.html" %}\n{% block title %}Welcome{% endblock %}\n{% block content %}\n'

def footerContent():
    return '\n{% endblock %}'

def allHtmls(templatePath):
    htmls = []
    for i in os.listdir(templatePath):
        if i[-4:] == 'html':
            htmls.append(i)
    return htmls

#--------------------

def main():
    gen('building')
    gen('person')
#    pass

if __name__ == "__main__":
    main()
